package dao.impl;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import dao.RowMapper;
import entity.Address;


public class JDBCTemplate {
	private static final String DRIVERNAME="com.mysql.jdbc.Driver";
	private static final String URL="jdbc:mysql://localhost:3306/lazyeggsystem";
	private static final String USERNAME="root";
	private static final String PASSWORD="root";
	
	static{
		try {
			Class.forName(DRIVERNAME);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	/**
	 * 获取连接
	 * @return
	 */
	public Connection getConnection(){
		Connection conn=null;
		try {
			conn=DriverManager.getConnection(URL, USERNAME, PASSWORD);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return conn;
	}
	/**
	 * 关闭
	 * @param rs
	 * @param pstm
	 * @param conn
	 */
	public void close(ResultSet rs,PreparedStatement pstm,Connection conn){
		if(rs!=null){
			try {
				rs.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		close(pstm, conn);
	}
	/**
	 * 关闭
	 * @param pstm
	 * @param conn
	 */
	public void close(PreparedStatement pstm, Connection conn) {
		if(pstm!=null){
			try {
				pstm.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if(conn!=null){
			try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	/**
	 * 增删改
	 * @param sql
	 * @param params
	 */
	public void CUDObject(String sql,Object...params){
		Connection conn=this.getConnection();
		java.sql.PreparedStatement pstm=null;
		try {
			pstm=conn.prepareStatement(sql);
			if(params!=null){
				for (int i = 0; i < params.length; i++) {
					pstm.setObject(i+1, params[i]);
				}
			}
			pstm.executeUpdate();
		} catch (SQLException e) {
			throw new DaoException("增删改异常",e);
		}finally{
			close(pstm, conn);
		}
	}
	/**
	 * 查询（返回List）
	 * @param sql
	 * @param rowMapper
	 * @param params
	 * @return
	 */
	public <T> List<T> queryAll(String sql,RowMapper<T> rowMapper,Object...params){
		List<T> list = new ArrayList<T>();
		Connection conn = this.getConnection();
		PreparedStatement pstm =null;
		ResultSet rs = null;
		try {
			pstm=conn.prepareStatement(sql);
			if(params!=null){
				for (int i = 0; i < params.length; i++) {
					pstm.setObject(i+1, params[i]);
				}
			}
			rs=pstm.executeQuery();
			T t = null;
			while(rs.next()){
				t=rowMapper.rowMapper(rs);
				list.add(t);
			}
		} catch (SQLException e) {
			throw new DaoException("查询异常",e);
		}finally{
			close(rs, pstm, conn);
		}
		return list.size()>0?list:null;
	}
	/**
	 * 查询（返回一个值）
	 * @param sql
	 * @param rowMapper
	 * @param params
	 * @return
	 */
	public <T> T queryOne(String sql,RowMapper<T> rowMapper,Object...params){
		T t = null;
		Connection conn =this.getConnection();
		PreparedStatement pstm = null;
		ResultSet rs = null;
		try {
			pstm=conn.prepareStatement(sql);
			if(params!=null){
				for (int i = 0; i < params.length; i++) {
					pstm.setObject(i+1, params[i]);
				}
			}
			rs=pstm.executeQuery();
			if(rs.next()){
				t=rowMapper.rowMapper(rs);
			}
		} catch (SQLException e) {
			throw new DaoException("查询异常",e);
		}finally{
			close(rs, pstm, conn);
		}
		
		return t;
	}
	/**
	 * 数据分页
	 * @param listT
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	public <T> List<T> queryByPaging(List<T> listT,int pageNum,int pageSize){
		List<T> list = new ArrayList<T>();
		int index=(pageNum-1)*pageSize;
		for (int i = 0; i < pageSize; i++) {
			try {
				list.add(listT.get(index));
			} catch (IndexOutOfBoundsException e) {
				return list.size()>0?list:null;
			}
			index++;
		}
		return list.size()>0?list:null;
	}
	/**
	 * 返回完整地址
	 * @param address
	 * @return
	 */
	public String queryCompleteAddress(Address address){
		StringBuilder comAddr = new StringBuilder();
		comAddr.append(address.getStreet().getArea().getAreaName());
		comAddr.append(address.getStreet().getStreetName());
		comAddr.append(address.getDetailAddress());
		return comAddr.toString();
	}
}
